package com.qf.serviceimpl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsStorage;
import com.qf.service.DtsStorageService;
import com.qf.util.AliyunOSSUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Ypl
 * @version V1.0
 * @Project September-mavens
 * @Package com.qf.serviceimpl
 * @Description:
 * @Date 2022/10/17 11:08
 */
@SuppressWarnings("all")
@Service
public class DtsStorageServiceImpl implements DtsStorageService {
    /**
     * Value 去获取properties中的数据
     */
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    @Value("${aliyun.oss.bucketName}")
    private String bucketName;

    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    @Autowired
    DtsStorageMapper dtsStorageMapper;

    /**
     * 分页查询
     */

    @Override
    public Map<String, Object> findByPage(Integer page, Integer limit, String sort, String order, String key, String name) {
        IPage<DtsStorage> iPage = new Page<>(page,limit);

        QueryWrapper<DtsStorage>  wrapper= new QueryWrapper<>();

        if(!StringUtils.isEmpty(key)){
            wrapper.like("`key`",key);
        }
        if(!StringUtils.isEmpty(name)){
            wrapper.like("name",name);
        }

        if("desc".equalsIgnoreCase(order)){
            wrapper.orderByDesc(sort);
        }else{
            wrapper.orderByAsc(sort);
        }

        wrapper.eq("deleted", 0);

        dtsStorageMapper.selectPage(iPage,wrapper);

        Map<String,Object> map = new HashMap<>();
        map.put("total",iPage.getTotal());
        map.put("items",iPage.getRecords());

        return map;

    }

    /**
     * 文件的上传
     */

    @Override
    public DtsStorage uploadFile(String name , int size , String type ,String objectName,byte[] bytes) throws Exception {
        
        //要选择回显URL的方法 上传图片到OSS
        String url = AliyunOSSUtil.uploadImage(endpoint, accessKeyId, accessKeySecret, bucketName,objectName , bytes);
        //存储到数据库
        DtsStorage dtsStorage = new DtsStorage();
        dtsStorage.setDeleted(false);
        dtsStorage.setAddTime(new Date());
        dtsStorage.setUpdateTime(new Date());
        dtsStorage.setKey(objectName);
        //文件大小
        dtsStorage.setSize(size);
        //文件的原始名字
        dtsStorage.setName(name);
        //图片类型
        dtsStorage.setType(type);
        dtsStorage.setUrl(url);
        dtsStorageMapper.insert(dtsStorage);

         return dtsStorage;
    }

    @Override
    public void delete(DtsStorage dtsStorage) {
        //删除数据库的信息
        dtsStorageMapper.deleteById(dtsStorage.getId());
        //删除阿里云上的信息
        AliyunOSSUtil.deleteFile(endpoint,accessKeyId,accessKeySecret,bucketName,dtsStorage.getKey());
    }

    /**
     * 对象编辑
     */
    @Override
    public DtsStorage update(DtsStorage dtsStorage) {

        dtsStorage.setUpdateTime(new Date());
        LambdaQueryWrapper<DtsStorage> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsStorage::getId,dtsStorage.getId());

        dtsStorageMapper.update(dtsStorage,wrapper);

        return dtsStorage;
    }
}
